package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import entity.tArea;
import entity.tUser;

import service.tAreaService;
import service.tUserService;

public class UpdatetUserAction {
	private String userId;
	private String userName;
	private String trueName;
	private String company;
	private String email;
	private String mobilePhone;
	private String phone;
	private Integer grade;
	private String tAreaName;
	private String test;
	private String newpass;
	private String result;
	private String oldpass;
	
	private tAreaService tareaService;
	private tUserService tuserService;
	
	public String update() throws IOException{
		
		ActionContext context = ActionContext.getContext();  
	    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);  
	    //HttpServletResponse response = (HttpServletResponse) context.get(ServletActionContext.HTTP_RESPONSE);  
	    @SuppressWarnings("unused")
		Map session = context.getSession(); 
		ServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		@SuppressWarnings("unused")
		PrintWriter out = response.getWriter();
		List<tUser> lists = new ArrayList<tUser>();
		System.out.println(userId+", userName:"+userName+", trueName:"+trueName+", email:"+email+", phone:"+phone+", mobilePhone:"+mobilePhone+", company:"+company);
		
		lists = tuserService.searchById(userId);
		
		if (phone.equals("")){
			result = "手机号为空，请重新输入";
			response.setContentType("text/xml;charset=utf-8"); //更改字符编码
			System.out.println(result);
			return "error";
		}
		
		if (mobilePhone.equals("")){
			result = "电话号码为空，请重新输入";
			response.setContentType("text/xml;charset=utf-8"); //更改字符编码
			System.out.println(result);
			return "error";
		}
		
		if (trueName.equals("")){
			result = "姓名为空，请重新输入";
			response.setContentType("text/xml;charset=utf-8"); //更改字符编码
			System.out.println(result);
			return "error";
		}
		
		if (email.equals("")){
			result = "邮箱为空，请重新输入";
			response.setContentType("text/xml;charset=utf-8"); //更改字符编码
			System.out.println(result);
			return "error";
		}
		
		List<tArea> tarea = new ArrayList<tArea>();
		System.out.println(test);
		System.out.println(test);
		
		tarea =  tareaService.searchByName(test);
		
		String tAreaId = tarea.get(0).getId();
		
		System.out.println(tAreaId);
		
		Date date=new Date();
		Timestamp ts = new Timestamp(date.getTime());
		
		tUser tuser = new tUser();
		
		String pwd = lists.get(0).getPwd();
		String weixin = lists.get(0).getWeixin();
		
		
		tuser.setUserId(userId);
		tuser.setUserName(userName);
		tuser.setTrueName(trueName);
		tuser.setEmail(email);
		tuser.setPhone(phone);
		tuser.setMobilePhone(mobilePhone);
		
		tuser.setPwd(pwd);		
		tuser.setWeixin(weixin);
		tuser.setGrade(grade);
		tuser.setAddTime(ts);
		tuser.settAreaId(tAreaId);
		//tuser.settDevice(null);
		
		System.out.println(tuser);
		
		tuserService.update(tuser);
		
		return "success";

	}
	public String updateperson() throws IOException{
		
		ActionContext context = ActionContext.getContext();  
	    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);  
	   
	    HttpSession session = ServletActionContext.getRequest().getSession();
		
		ServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		@SuppressWarnings("unused")
		PrintWriter out = response.getWriter();
		List<tUser> lists = new ArrayList<tUser>();
		System.out.println(userId+", userName:"+userName+", trueName:"+trueName+", email:"+email+", phone:"+phone+", mobilePhone:"+mobilePhone+", company:"+company);
		
		if (phone.equals("")){
			result = "手机号为空，请重新输入";
			response.setContentType("text/xml;charset=utf-8"); //更改字符编码
			System.out.println(result);
			return "error";
		}
		
		if (mobilePhone.equals("")){
			result = "电话号码为空，请重新输入";
			response.setContentType("text/xml;charset=utf-8"); //更改字符编码
			System.out.println(result);
			return "error";
		}
		
		if (trueName.equals("")){
			result = "姓名为空，请重新输入";
			response.setContentType("text/xml;charset=utf-8"); //更改字符编码
			System.out.println(result);
			return "error";
		}
		
		if (email.equals("")){
			result = "邮箱为空，请重新输入";
			response.setContentType("text/xml;charset=utf-8"); //更改字符编码
			System.out.println(result);
			return "error";
		}
		
		
		
		lists = tuserService.searchById(userId);
		
		tUser tuser = new tUser();
		
		Date date=new Date();
		Timestamp ts = new Timestamp(date.getTime());
		
		String pwd = lists.get(0).getPwd();
		String weixin = lists.get(0).getWeixin();
		String tAreaId = lists.get(0).gettAreaId();
		Integer Grade = lists.get(0).getGrade();
		
		tuser.setUserId(userId);
		tuser.setUserName(userName);
		tuser.setTrueName(trueName);
		tuser.setEmail(email);
		tuser.setPhone(phone);
		tuser.setMobilePhone(mobilePhone);
		//tuser.setCompany(company);		
		tuser.setPwd(pwd);		
		tuser.setWeixin(weixin);
		tuser.setGrade(Grade);
		tuser.setAddTime(ts);
		tuser.settAreaId(tAreaId);
		//tuser.settDevice(null);
		
		System.out.println(tuser);
		
		tuserService.update(tuser);
		System.out.println("------------------------------------");
		
		session.setAttribute("user", tuser);
		
		return "success";
	}
	
	public String alterpassword() throws IOException{
		
		ActionContext context = ActionContext.getContext();  
	    HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);  
	   
	    HttpSession session = ServletActionContext.getRequest().getSession();
		
		ServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		@SuppressWarnings("unused")
		PrintWriter out = response.getWriter();
		List<tUser> lists = new ArrayList<tUser>();
		
		lists = tuserService.searchById(userId);
		
		System.out.println(userId+" -+=#$&~|~&$#=+-  "+newpass);
		
		String pwd = lists.get(0).getPwd();
		System.out.println(pwd);
		System.out.println(oldpass);
		
		if ( ! pwd.equals(oldpass)){
			result = "您输入的原始密码与该账号密码不符，如忘记密码请联系管理员！";
			response.setContentType("text/xml;charset=utf-8"); //更改字符编码
			System.out.println(result);
			return "error";
		}
		
		String weixin = lists.get(0).getWeixin();
		String tAreaId = lists.get(0).gettAreaId();
		Integer Grade = lists.get(0).getGrade();
		String userName = lists.get(0).getUserName();
		String trueName = lists.get(0).getTrueName();
		String email = lists.get(0).getEmail();
		String phone = lists.get(0).getPhone();
		String mobilePhone = lists.get(0).getMobilePhone();
		
		tUser tuser = new tUser();
		
		Date date=new Date();
		Timestamp ts = new Timestamp(date.getTime());
		
		tuser.setUserId(userId);
		tuser.setUserName(userName);
		tuser.setTrueName(trueName);
		tuser.setEmail(email);
		tuser.setPhone(phone);
		tuser.setMobilePhone(mobilePhone);
		//tuser.setCompany(company);		
		//tuser.setPwd(pwd);		
		tuser.setWeixin(weixin);
		tuser.setGrade(Grade);
		tuser.setAddTime(ts);
		tuser.settAreaId(tAreaId);
		
		
		
		
		//tuser.setUserId(userId);
		tuser.setPwd(newpass);
		
		tuserService.update(tuser);
		
		return "success";
	}

	public tUserService getTuserService() {
		return tuserService;
	}

	public void setTuserService(tUserService tuserService) {
		this.tuserService = tuserService;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getTrueName() {
		return trueName;
	}

	public void setTrueName(String trueName) {
		this.trueName = trueName;
	}

	public String getCompany() {
		return company;
	}

	public void setCompany(String company) {
		this.company = company;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	

	public String getMobilePhone() {
		return mobilePhone;
	}

	public void setMobilePhone(String mobilePhone) {
		this.mobilePhone = mobilePhone;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public Integer getGrade() {
		return grade;
	}

	public void setGrade(Integer grade) {
		this.grade = grade;
	}

	public String gettAreaName() {
		return tAreaName;
	}

	public void settAreaName(String tAreaName) {
		this.tAreaName = tAreaName;
	}

	public tAreaService getTareaService() {
		return tareaService;
	}

	public void setTareaService(tAreaService tareaService) {
		this.tareaService = tareaService;
	}

	public String getTest() {
		return test;
	}

	public void setTest(String test) {
		this.test = test;
	}
	public String getNewpass() {
		return newpass;
	}
	public void setNewpass(String newpass) {
		this.newpass = newpass;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public String getOldpass() {
		return oldpass;
	}
	public void setOldpass(String oldpass) {
		this.oldpass = oldpass;
	}
	
	

}
